package com.itheima.domain;

import com.itheima.domain.impl.MyTopic;
import com.itheima.domain.impl.MyTopicSubscriber;

/**
 * @ClassName: ObserverPatternTest
 * @Description:
 * @author: Yrd
 * @date: 2021-11-02 14:57
 */
public class ObserverPatternTest {
    public static void main(String[] args){
        //create subject
        MyTopic myTopic = new MyTopic();

        //create observers
        Observer observer1 = new MyTopicSubscriber("obj1",myTopic);
        Observer observer2 = new MyTopicSubscriber("obj2",myTopic);
        Observer observer3 = new MyTopicSubscriber("obj3",myTopic);
        Observer observer4 = new MyTopicSubscriber("obj4",myTopic);

        //register observers to the subject
        myTopic.register(observer1);
        ((MyTopicSubscriber) observer2).registerObserver();
        myTopic.register(observer3);

        //attach observer to subject
        observer1.setSubject(myTopic);

        //check if any update is available
        observer1.update();

        System.out.println("线程名称："+Thread.currentThread().getName());
        //now send message to subject
        myTopic.postMessage("666");


    }
}


